package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.UserModel;
import com.slwish.xty.mvp.contract.ModifyNameContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.exception.ApiException;

/**
 * 模块说明：修改昵称
 *
 * @author 陈浩
 * @date 2017/12/15
 */

public class ModifyNamePresenter extends BasePresenter<ModifyNameContract.View> implements ModifyNameContract.Presenter {
    public ModifyNamePresenter(ModifyNameContract.View view) {
        super(view);
    }

    /**
     * 修改昵称
     *
     * @param context
     * @param nickName
     * @param accessToken
     */
    @Override
    public void modify(Context context, String nickName, String accessToken) {
        if (TextUtils.isEmpty(nickName)) {
            setError(R.string.toast_user_info_modify_nickname_not_null);
            return;
        }
        if (TextUtils.isEmpty(accessToken)) {
            setError(R.string.toast_user_info_modify_nickname_token_not_null);
            return;
        }
        EasyHttp.post(UrlConfig.MODIFY_NICKNAME_URL)
                .params("nickName", nickName)
                .params("accessToken", accessToken)
                .execute(new ProgressDialogCallBack<String>(showProgressDialog(context)) {
                    @Override
                    public void onSuccess(String json) {
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String user = jsonObject.getString("user");
                            //return
                            setError(resultMessage);
                            if (!TextUtils.equals(resultCode, "0"))
                                return;
                            UserModel userModel = JSON.parseObject(user, UserModel.class);
                            if (userModel != null && getView() != null) {
                                //更新本地数据库
                                userModel.saveOrUpdate("userid = ?", userModel.getUserId() + "");
                                getView().modifySuccess(userModel);
                            }
                        } catch (Exception e) {
                            setError(R.string.toast_user_info_modify_nickname_failed);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        setError(R.string.toast_user_info_modify_nickname_exception);
                    }
                });
    }
}
